<?php
namespace app\admin\controller;
use app\Base;
use think\facade\Db;
class Cat extends Base{
	
    public function index()
    {
		if(!$this->request->post('pageSize') || !is_int($this->request->post('pageSize')))
		{
			$this->kerror('参数错误');
		}
		
		$pageSet=array('page'=>$this->request->post('page'),'list_rows'=>$this->request->post('pageSize'));
		
		$dataList=Db::name('cat')->where('parentid',0)->order("listorder","asc")->paginate($pageSet)->each(function($item,$key){
			$children=Db::name('cat')->where('parentid',$item['catid'])->order("listorder","asc")->select()->toArray();
			if($children)
			{			
				foreach($children as $key=>$val)
				{
					$children[$key]['total']=0;
					if($val['type']==1)
					{
						$children[$key]['total']=Db::name('news')->where("catid",$val['catid'])->count();
					}
				}
				$item['children']=$children;
			}
			$item['total']=0;
			if($item['type']==1)
			{
				$item['total']=Db::name('news')->where("catid",$item['catid'])->count();
				
			}
			 
			return $item;
		})->toArray()['data'];
		
		$total=Db::name('cat')->where('parentid',0)->count();
		
		$this->setobj(array('dataList'=>$dataList,'total'=>$total));
				
		return $this->ok();
    }
	
	public function addCat()
	{
		$info=$this->request->post();
		if(!$this->request->post('catname'))
		{
			return $this->kerror('参数错误');
		}
		$data=array('type'=>$this->request->post('type'),'catname'=>$this->request->post('catname'),'parentid'=>$this->request->post('parentid'),'description'=>$this->request->post('description'),'content'=>$this->request->post('content'));
		$catid=Db::name('cat')->insert($data);
		$files=$this->request->post('imgs');
		if($files)
		{
			$vkey=md5("catid=".$catid);
			foreach($files as $key=>$val)
			{
				$arr=array('name'=>$vkey,'file'=>$val);
				Db::name('file')->insert($arr);
			}
		}
		return $this->ok();
	}
	
	public function editCat()
	{
		
		if(!$this->request->post('catid'))
		{
			return $this->kerror('参数错误');
		}
		$catid=$this->request->post('catid');
		$info=Db::name('cat')->where("catid",$catid)->find();
		
		if($this->request->post('catname'))
		{	
			
			$data=array('catid'=>$catid,'catname'=>$this->request->post('catname'),'description'=>$this->request->post('description'),'content'=>$this->request->post('content'));
			Db::name('cat')->update($data);			
			$files=$this->request->post('imgs');
			if($files)
			{
				$vkey=md5("catid=".$catid);
				foreach($files as $key=>$val)
				{
					$arr=array('name'=>$vkey,'file'=>$val);
					Db:name('file')->insert($arr);
				}
			}
			return $this->ok();
		}
		else{
			$this->setobj($info);
			return $this->ok();
		}
	}
	
	public function topCats()
	{
		$dataList=Db::name('cat')->where("parentid",0)->order("catid","asc")->select();
		$this->setobj($dataList);
		return $this->ok();
	}
	
	public function delCat()
	{
		$catid=$this->request->post('catid');
		$info=Db::name('cat')->where("catid",$catid)->find();
		Db::name('cat')->where("catid",$catid)->delete();
		return $this->ok();
	}
	
	public function orderCat()
	{
		$catid=$this->request->post('catid');
		$order=$this->request->post('listorder');
		 
		
		if(!$catid)
		{
			return $this->kerror('参数错误');
		}
		
		
		Db::name('cat')->where("catid",$catid)->update(array('listorder'=>$order));
		
		return $this->ok();
	}
	
	private function delNews($cat)
	{
		//先删除本分类,再递归
		
	}
	 
	
}
